package com.example.homework;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.homework.dao.UserDao;
import com.example.homework.pojo.User;

public class RegisterActivity extends AppCompatActivity {
    EditText register_username, register_password, register_confirmPassword;
    Button user_register, back_login;
    UserDao userDao;
    CheckBox agreement;
    TextView user_agreement, user_privacy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        register_username = findViewById(R.id.register_username);
        register_password = findViewById(R.id.register_password);
        register_confirmPassword = findViewById(R.id.register_confirmPassword);
        user_register = findViewById(R.id.user_register);
        back_login = findViewById(R.id.back_login);
        agreement = findViewById(R.id.agreement);

        userDao = new UserDao(this);


        /**
         * 注册按钮的点击事件
         */
        user_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = register_username.getText().toString();
                String password = register_password.getText().toString();
                String password1 = register_confirmPassword.getText().toString();

                User user = new User();
                user.setUsername(username);
                user.setPassword(password);

                /**
                 * 实现用户注册功能
                 * 包括用户名密码插入数据库以及用户名查重
                 */
                if (username.length() == 0 || password.length() == 0 || password1.length() == 0) {
                    Toast.makeText(RegisterActivity.this, "用户名和密码均不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (password.equals(password1)) {
                        //用户名查重
                        boolean duplicateUsername = userDao.DuplicateUsername(user);
                        if (duplicateUsername) {
                            long insertData = userDao.insertData(user);
                            if (insertData != -1) {
                                Toast.makeText(RegisterActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(RegisterActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(RegisterActivity.this, "该用户名已经被注册，请更换用户名重新注册", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(RegisterActivity.this, "两次输入的密码不一致，请重试！", Toast.LENGTH_SHORT).show();
                        register_password.setText("");
                        register_confirmPassword.setText("");
                    }
                }
            }
        });

        /**
         * 返回按键的点击事件
         * 点击返回登录界面
         */
        back_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterActivity.super.onBackPressed();
            }
        });


    }
}